package varredor.concorrencia;

public class Semaforo {

	private int mutex = 1;

	private String nome = "";
	private boolean debug = true;

	public Semaforo() {
		nome = "" + this;
	}

	public synchronized void DOWN() {
		if (debug)
			System.out.println("DOWN " + nome);

		while (mutex != 1) {
			try {
				Thread.currentThread().sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		mutex = 0;
	}

	public void UP() {
		if (debug)
			System.out.println("UP " + nome);

		mutex = 1;
	}

	public void setMutex(int mutex) {
		this.mutex = mutex;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

}
